package com.shd.edanpei.customer.presenter;

import android.app.Dialog;
import android.text.TextUtils;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.PayModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.customer.bean.PayInfoModel;
import com.shd.edanpei.customer.contract.PayContract;
import com.shd.edanpei.enums.PaymentEnum;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.pay.PayUtils;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */
public class PayPresenter extends BasePresenterImpl<PayContract.PayIView> implements PayContract.PayIPresenter {

    private Dialog mDialog;

    //记录以下四个参数，微信支付成功回调时使用
    private PaymentEnum mPaymentEnum;
    private Constant.PayType mPayType;
    private int mOrderId, mIndex;


    public PayPresenter(PayContract.PayIView mView) {
        super(mView);
    }

    @Override
    public void getData(int orderId) {
        ApiCreator.getApi().getPayDetails(orderId)
                .compose(RxTransformer.<PayInfoModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PayInfoModel>() {
                    @Override
                    protected void onSuccess(PayInfoModel data) {
                        mBaseIView.setData(data);
                    }
                });
    }

    @Override
    public void payDeposit(final int orderId, final PaymentEnum paymentEnum, final int index) {
        final WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
        hashMap.put(ApiParams.ORDER_ID, orderId);
        hashMap.put(ApiParams.PAY_TYPE, paymentEnum.getType());
        ApiCreator.getApi().payDeposit(hashMap)
                .compose(RxTransformer.<PayModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PayModel>() {
                    @Override
                    protected void onSuccess(PayModel data) {
                        if (TextUtils.isEmpty(data.getErrorMsg())) {
                            if (paymentEnum == PaymentEnum.alipay) {
                                //支付宝支付
                                PayUtils.getInstance().aliPay(mBaseIView.getContext(), data.getAlipay(), new PayUtils.AliPayListener() {
                                    @Override
                                    public void success() {
                                        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.applying.getStatus()));
                                        mBaseIView.getContext().finish();
//                                        toPayResult(paymentEnum, Constant.PayType.deposit, orderId);
                                    }

                                    @Override
                                    public void failed() {
                                        ToastUtil.showToast(PayUtils.PAY_FAILED);
                                    }
                                });
                            }
                            if (paymentEnum == PaymentEnum.wechat) {
                                //微信支付
                                mPaymentEnum = paymentEnum;
                                mPayType = Constant.PayType.deposit;
                                mOrderId = orderId;
                                mIndex = index;
                                PayUtils.getInstance().wechatPay(mBaseIView.getContext(), data.getWechat());
                            }
                        } else {
                            ToastUtil.showToast(data.getErrorMsg());
                        }

                    }
                });
    }


    /**
     * 微信支付成功回调
     */
    public void weChatPaySuccess() {
        //刷新外层列表数据
//        if (mPayType.getType() == Constant.PayType.deposit.getType()) {
//            RxBus.get().send(RxBusCode.NOTIFY_LOAN_ADAPTER, new NotifyBean(mIndex, StateEnum.applying));
//        } else if (mPayType.getType() == Constant.PayType.deposit.getType()) {
//            RxBus.get().send(RxBusCode.NOTIFY_LOAN_ADAPTER, new NotifyBean(mIndex, StateEnum.wait_loan));
//        }
//        toPayResult(mPaymentEnum, mPayType, mOrderId);
    }

}
